package com.randev.snapgallery;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.randev.snap.R;
import com.randev.snapconfig.Config;

public class ImageGalleryActivity extends Activity {

	String folderName = "";
	boolean EXTERNAL_STORAGE_AVAILABLE = false;
	GridView gViewImages;
	TextView tViewFolderName;
	ImageGalleryAdapter imageGalleryAdapter;
	private final String TAG = "ImageGalleryActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_folder);
		
		gViewImages = (GridView) findViewById(R.id.gvImages);
		tViewFolderName = (TextView) findViewById(R.id.tvParentDirectory);
		EXTERNAL_STORAGE_AVAILABLE = isExternalStorageWritable();
		getDataFromPreviousActivity();
		setAdapterToGridView();
		tViewFolderName.setText(folderName);
		
		gViewImages.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				List<String> fileNames = getFileNames(folderName);
				String selectedImageName = fileNames.get(arg2);
				Bundle bundle = new Bundle();
				bundle.putString(Config.FOLDER_ID, folderName);
				bundle.putString(Config.IMAGE_NAME, selectedImageName);
				try {
					Intent imageViewIntent = new Intent(ImageGalleryActivity.this, ImageViewActivity.class);
					imageViewIntent.putExtras(bundle);
					startActivity(imageViewIntent);
				} catch (ActivityNotFoundException e) {
					Log.w(TAG, "Unable to start ImageViewActivity :" + e.toString());
				}
			}
		});
		
		
	}

	private void getDataFromPreviousActivity() {
		try {
			Bundle extras = getIntent().getExtras();
			folderName = extras.getString(Config.FOLDER_ID);
		} catch (Exception e) {
			Log.w(TAG, "Unable to get data from previous activity: " + e.toString());
		}
		
	}

	private void setAdapterToGridView() {
		List<String> fileNames = getFileNames(folderName);
		imageGalleryAdapter = new ImageGalleryAdapter(this, folderName, fileNames);
		gViewImages.setAdapter(imageGalleryAdapter);
		
	}
	
	private List<String> getFileNames(String folderName){
		List<String> fileNames = new ArrayList<String>();
		
		if (EXTERNAL_STORAGE_AVAILABLE) {
			File imageFolderFile = new File(Config.HOME_DIRECTORY+File.separator+folderName);
			if (imageFolderFile.exists()) {
				fileNames = Arrays.asList(imageFolderFile.list());
			}
		}
		return fileNames;
		
	}
	
	private boolean isExternalStorageWritable() {
		boolean isMediaAvailable = false;
	    String state = Environment.getExternalStorageState();
	    if (Environment.MEDIA_MOUNTED.equals(state)) {
	    	isMediaAvailable =  true;
	    }
	    return isMediaAvailable;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	    	try {
				Intent galleryActivityIntent = new Intent(ImageGalleryActivity.this, GalleryActivity.class);
				finish();
				startActivity(galleryActivityIntent);
	    	} catch (ActivityNotFoundException e) {
				Log.w(TAG, "Unable to start GalleryActivity :" + e.toString());
			}
	    	return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}

}
